package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.OrderStateEnum;
import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsMainOrderQueryPageRequest;
import com.sgzn.sgpay.model.response.CurrentPageInfo;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.MainOrderQueryPageResult;
import com.sgzn.sgpay.model.response.vo.MainOrderVo;
import com.sgzn.sgpay.persistence.dao.TMainOrderMapper;
import com.sgzn.sgpay.persistence.domain.TMainOrder;
import com.sgzn.sgpay.persistence.domain.TMainOrderExample;
import com.sgzn.sgpay.service.SgsMainOrderService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.util.PageCalculator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 主订单管理服务接口实现类
 * @author huangsenguang
 * @version Id: SgsMainOrderServiceImpl.java, v 0.1 2018/4/25 21:02 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsMainOrderServiceImpl implements SgsMainOrderService {
    @Autowired
    private TMainOrderMapper tMainOrderMapper;

    @Override
    public SgsResultResponse<MainOrderQueryPageResult> queryPage(SgsMainOrderQueryPageRequest request) {
        try{
            TMainOrderExample example = this.getTMainOrderExampleByPage(request);
            int totalCount = this.tMainOrderMapper.countByExample(example);//获取总数
            CurrentPageInfo pageInfo = PageCalculator.getPageInfo(totalCount, request.getPageQueryParam().getPageSize(),
                    request.getPageQueryParam().getCurrentPage());
            StringBuilder clause = new StringBuilder();
            //加上起始和结束行号进行列表查询
            clause.append("created_at desc ");
            clause.append("limit ").append(pageInfo.getStartIndex()).
                    append(",").append(pageInfo.getPageSize());
            example.setOrderByClause(clause.toString());
            List<TMainOrder> list = tMainOrderMapper.selectByExample(example);
            log.info("SgsMainOrderServiceImpl queryPage list:{}", list);
            return SgsResponseUtil.INSTANCE.success(getResult(totalCount, pageInfo, list));
        }catch (Exception e){
            log.error("门店分页查询系统异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503.getResponseCode(),SgsResponseCode.C503.getResponseDesc());
        }
    }

    private MainOrderQueryPageResult getResult(int totalCount, CurrentPageInfo pageInfo, List<TMainOrder> list) {
        MainOrderQueryPageResult result = new MainOrderQueryPageResult();
        result.setMainOrderList(convertToVo(list));
        result.setCurrentPage(pageInfo.getCurrentPage());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(totalCount);
        result.setTotalPage(pageInfo.getTotalPage());
        return result;
    }

    private List<MainOrderVo> convertToVo(List<TMainOrder> list) {
        List<MainOrderVo> voList = new ArrayList<>();
        for (TMainOrder tMainOrder : list) {
            MainOrderVo vo = new MainOrderVo();
            vo.setOrderId(tMainOrder.getOrderId());
            vo.setOrderType(tMainOrder.getOrderType());
            vo.setOrderState(tMainOrder.getOrderState());
            vo.setOrderStateName(OrderStateEnum.explain(tMainOrder.getOrderState()));
            vo.setOrderAmount(tMainOrder.getOrderAmount());
            vo.setUserId(tMainOrder.getUserId());
            vo.setCreatedAt(tMainOrder.getCreatedAt());
            vo.setUpdatedAt(tMainOrder.getUpdatedAt());
            vo.setCreatedBy(tMainOrder.getCreatedBy());
            vo.setUpdatedBy(tMainOrder.getUpdatedBy());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 获取查询对象
     * @param request
     * @return
     */
    private TMainOrderExample getTMainOrderExampleByPage(SgsMainOrderQueryPageRequest request){
        TMainOrderExample countExample = new TMainOrderExample();
        TMainOrderExample.Criteria cra = countExample.createCriteria();
        if(StringUtils.isNotBlank(request.getOrderId())){
            cra.andOrderIdEqualTo(request.getOrderId());
        }
        if(StringUtils.isNotBlank(request.getOrderType())){
            cra.andOrderTypeEqualTo(request.getOrderType());
        }
        if(StringUtils.isNotBlank(request.getOrderState())){
            cra.andOrderStateEqualTo(request.getOrderState());
        }
        if(StringUtils.isNotBlank(request.getUserId())){
            cra.andUserIdEqualTo(request.getUserId());
        }
        return countExample;
    }
}

